package com.example.jack.community.view.me;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.percent.PercentRelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.jack.community.R;
import com.example.jack.community.view.me.collect.MyCollActivity;
import com.example.jack.community.view.me.per_setting.Setting_iconActivity;
import com.example.jack.community.view.me.personinfo.Per_infoActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.content.Context.MODE_PRIVATE;


/**
 * 我的界面
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment {


    @BindView(R.id.user_logo_layout)
    PercentRelativeLayout userLogoLayout;
    @BindView(R.id.setting_icon)
    ImageView settingIcon;
    @BindView(R.id.setting_user_name)
    TextView settingUserName;

    @BindView(R.id.fill_b)
    TextView fillB;
    @BindView(R.id.card_a)
    CardView cardA;
    @BindView(R.id.card_c)
    CardView cardC;
    @BindView(R.id.setting_activity)
    PercentRelativeLayout settingActivity;
    Unbinder unbinder;
    @BindView(R.id.card_b)
    CardView cardB;
    @BindView(R.id.setting_user_icon)
    ImageView settingUserIcon;

    private static final String TAG = "TonJie";
    public static MeFragment newInstance(String s1) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", s1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.me_fragment_main, container, false);
        unbinder = ButterKnife.bind(this, view);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("data", MODE_PRIVATE);
        String user_logo = sharedPreferences.getString("user_logo", "0");
        String user_Name = sharedPreferences.getString("studentName", "0");

        if (user_logo.equals("0")) {
            Glide.with(this).load(R.drawable.login_users).
                    into(settingUserIcon);
            Log.d(TAG, "onCreateView: 等于0");
        }else{
            Glide.with(this).load(user_logo).
                    into(settingUserIcon);
            Log.d(TAG, "onCreateView: 不等于0");
        }

        if(user_Name.equals("0")){
            settingUserName.setText("username");
        }else{
            settingUserName.setText(user_Name);
        }
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("data", MODE_PRIVATE);
        String user_logo = sharedPreferences.getString("user_logo", "0");

        if (!user_logo.equals("0")) {
            Glide.with(this).load(user_logo).into(settingUserIcon);
        }
        String user_Name = sharedPreferences.getString("studentName", "0");
        if(user_Name.equals("0")){
            settingUserName.setText("username");
        }else{
            settingUserName.setText(user_Name);
        }
    }

    @OnClick({R.id.card_b, R.id.setting_icon, R.id.setting_user_icon, R.id.card_a, R.id.card_c})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.card_b:
                startActivity(new Intent(getActivity(), MyCollActivity.class));
                break;
            case R.id.setting_icon:
                startActivity(new Intent(getActivity(), Setting_iconActivity.class));
                break;
            case R.id.setting_user_icon:
                startActivity(new Intent(getActivity(), Per_infoActivity.class));
                break;
            case R.id.card_a:
                startActivity(new Intent(getActivity(), My_Com_Activity.class));
                break;
            case R.id.card_c:
                Toast.makeText(getActivity(), "我的相册功能暂时未开放,敬请期待", Toast.LENGTH_SHORT).show();
                break;
        }
    }

}
